{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Crawler Log Folder"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minOperations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #文件夹操作日志搜集器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>每当用户执行变更文件夹操作时，LeetCode 文件系统都会保存一条日志记录。</p>\n",
    "\n",
    "<p>下面给出对变更操作的说明：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>&quot;../&quot;</code> ：移动到当前文件夹的父文件夹。如果已经在主文件夹下，则 <strong>继续停留在当前文件夹</strong> 。</li>\n",
    "\t<li><code>&quot;./&quot;</code> ：继续停留在当前文件夹<strong>。</strong></li>\n",
    "\t<li><code>&quot;x/&quot;</code> ：移动到名为 <code>x</code> 的子文件夹中。题目数据 <strong>保证总是存在文件夹 <code>x</code></strong> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个字符串列表 <code>logs</code> ，其中 <code>logs[i]</code> 是用户在 <code>i<sup>th</sup></code> 步执行的操作。</p>\n",
    "\n",
    "<p>文件系统启动时位于主文件夹，然后执行 <code>logs</code> 中的操作。</p>\n",
    "\n",
    "<p>执行完所有变更文件夹操作后，请你找出 <strong>返回主文件夹所需的最小步数</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/sample_11_1957.png\" style=\"height: 151px; width: 775px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>logs = [&quot;d1/&quot;,&quot;d2/&quot;,&quot;../&quot;,&quot;d21/&quot;,&quot;./&quot;]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>执行 &quot;../&quot; 操作变更文件夹 2 次，即可回到主文件夹\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/09/26/sample_22_1957.png\" style=\"height: 270px; width: 600px;\"></p>\n",
    "\n",
    "<pre><strong>输入：</strong>logs = [&quot;d1/&quot;,&quot;d2/&quot;,&quot;./&quot;,&quot;d3/&quot;,&quot;../&quot;,&quot;d31/&quot;]\n",
    "<strong>输出：</strong>3\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>logs = [&quot;d1/&quot;,&quot;../&quot;,&quot;../&quot;,&quot;../&quot;]\n",
    "<strong>输出：</strong>0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= logs.length &lt;= 10<sup>3</sup></code></li>\n",
    "\t<li><code>2 &lt;= logs[i].length &lt;= 10</code></li>\n",
    "\t<li><code>logs[i]</code> 包含小写英文字母，数字，<code>&#39;.&#39;</code> 和 <code>&#39;/&#39;</code></li>\n",
    "\t<li><code>logs[i]</code> 符合语句中描述的格式</li>\n",
    "\t<li>文件夹名称由小写英文字母和数字组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [crawler-log-folder](https://leetcode.cn/problems/crawler-log-folder/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [crawler-log-folder](https://leetcode.cn/problems/crawler-log-folder/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"d1/\",\"d2/\",\"../\",\"d21/\",\"./\"]', '[\"d1/\",\"d2/\",\"./\",\"d3/\",\"../\",\"d31/\"]', '[\"d1/\",\"../\",\"../\",\"../\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        top=0\n",
    "        for i in logs:\n",
    "            if i!=\"../\" and i!=\"./\":\n",
    "                top+=1\n",
    "            elif i==\"../\" and top>0:\n",
    "                top-=1\n",
    "        return top\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        depth = 0\n",
    "        for log in logs:\n",
    "            if log == \"./\":\n",
    "                continue\n",
    "            if log != \"../\":\n",
    "                depth += 1\n",
    "            elif depth:\n",
    "                depth -= 1\n",
    "        return depth\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        n=0\n",
    "        for i in logs:\n",
    "            if i ==\"../\":\n",
    "                n=max(n-1,0)\n",
    "            elif i ==\"./\":\n",
    "                n+=0\n",
    "            else:\n",
    "                n+=1\n",
    "        return max(n,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        n = len(logs)\n",
    "        dep = 0\n",
    "        for i in range(n):\n",
    "            if logs[i] == \"../\":\n",
    "                if dep > 0:\n",
    "                    dep -= 1\n",
    "            elif logs[i] != \"./\":\n",
    "                dep += 1\n",
    "        return dep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        stack = []\n",
    "        for x in logs:\n",
    "            if stack and x == \"../\":\n",
    "                stack.pop()\n",
    "            elif x != \"./\" and x != \"../\":\n",
    "                stack.append(x)\n",
    "        return len(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(logs)):\n",
    "            if logs[i] == './':\n",
    "                continue\n",
    "            if logs[i] == '../':\n",
    "                ans -= 1\n",
    "                if ans < 0:\n",
    "                    ans = 0\n",
    "            else:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        stack = []\n",
    "        for i in logs:\n",
    "            if i.strip('/') == '.':\n",
    "                pass\n",
    "            elif i.strip('/') == '..':\n",
    "                if len(stack) >= 1:\n",
    "                    stack.pop()\n",
    "                else:\n",
    "                    pass\n",
    "            else:\n",
    "                stack.append(i.strip('/'))\n",
    "        return len(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        cur = 0\n",
    "        for log in logs:\n",
    "            match log:\n",
    "                case \"../\":\n",
    "                    cur = max(cur - 1, 0)\n",
    "                case \"./\":\n",
    "                    pass\n",
    "                case _:\n",
    "                    cur += 1\n",
    "        return cur\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        top=0\n",
    "        for i in logs:\n",
    "            if i!=\"../\" and i!=\"./\":\n",
    "                top+=1\n",
    "            elif i==\"../\" and top>0:\n",
    "                top-=1\n",
    "        return top\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        self.index = 0\n",
    "        for d in logs:\n",
    "            if d == \"../\":\n",
    "                self.index -= 1\n",
    "                if self.index < 0:\n",
    "                    self.index = 0\n",
    "            elif d == \"./\":\n",
    "                pass\n",
    "            else:\n",
    "                self.index += 1\n",
    "        return self.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        res = 0\n",
    "        oper_dict = {\"./\": 0, \"../\": -1}\n",
    "        for li in logs:\n",
    "            res = max(0, res + oper_dict.get(li, 1))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        a = 0\n",
    "        for s in logs:\n",
    "            if s == '../':\n",
    "                if a>0:\n",
    "                    a-=1\n",
    "                else:\n",
    "                    a = 0\n",
    "            elif s=='./':\n",
    "                continue\n",
    "            else:\n",
    "                a+=1\n",
    "        return a "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for i in logs:\n",
    "            if i == \"./\":\n",
    "                continue\n",
    "            if i != \"../\":\n",
    "                ans += 1\n",
    "            elif ans:\n",
    "                ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "      ans = 0\n",
    "      for i in logs:\n",
    "        if i == '../' and ans !=0:\n",
    "          ans-=1\n",
    "          continue\n",
    "        if i == './':\n",
    "          continue\n",
    "        if i!='./' and i!='../':\n",
    "          ans+=1\n",
    "      return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for l in logs:\n",
    "            if l[0] != \".\":\n",
    "                ans += 1\n",
    "            elif l == \"../\":\n",
    "                ans -= 1 if ans > 0 else 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        fh = 0\n",
    "        for i in logs:\n",
    "            match i:\n",
    "                case \"../\":\n",
    "                    if fh > 0:\n",
    "                        fh -= 1\n",
    "                case \"./\":\n",
    "                    pass\n",
    "                case _: \n",
    "                    fh += 1 \n",
    "        return fh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        res = 0\n",
    "        for log in logs:\n",
    "            if log == '../':\n",
    "                if res != 0:\n",
    "                    res -= 1\n",
    "            elif log != './':\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(logs)):\n",
    "            if logs[i] == './':\n",
    "                continue\n",
    "            if logs[i] != '../':\n",
    "                ans += 1\n",
    "            elif ans:\n",
    "                ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        depth = 0\n",
    "        for i in range(0,len(logs)):\n",
    "            if logs[i]=='../':\n",
    "                if depth>0:\n",
    "                    depth -= 1\n",
    "            else:\n",
    "                if logs[i]!='./':\n",
    "                    depth += 1 \n",
    "        return depth\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        retVal = 0\n",
    "\n",
    "        for log in logs:\n",
    "            if log == \"../\":\n",
    "                if retVal > 0:\n",
    "                    retVal -= 1\n",
    "            elif log == \"./\":\n",
    "                continue\n",
    "            else:\n",
    "                retVal += 1\n",
    "\n",
    "        return retVal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        cnt = 0\n",
    "        for ele in logs:\n",
    "            if ele == './':\n",
    "                pass\n",
    "            elif ele == '../':\n",
    "                cnt = max(0, cnt-1)\n",
    "            else:\n",
    "                cnt += 1\n",
    "        return cnt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        out = 0\n",
    "        for i in logs:\n",
    "            if i == '../' :\n",
    "                if out != 0:\n",
    "                    out -= 1\n",
    "            elif i == './':\n",
    "                out = out\n",
    "            else:\n",
    "                out += 1\n",
    "        return out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        c=0\n",
    "        for i in logs:\n",
    "            if i=='../':\n",
    "                if c>0:\n",
    "                    c-=1\n",
    "            elif i=='./':\n",
    "                pass\n",
    "            else:\n",
    "                c+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        depth = 0\n",
    "        for log in logs:\n",
    "            if log == './' or (log == '../' and depth == 0):\n",
    "                pass\n",
    "            elif log != './' and log != '../':\n",
    "                depth += 1\n",
    "            elif log == '../' and depth != 0:\n",
    "                depth -= 1\n",
    "        return depth\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        stack = []\n",
    "        for log in logs:\n",
    "            if log != \"./\" and log != \"../\":\n",
    "                stack.append(log)\n",
    "            elif log == \"../\" and stack:\n",
    "                stack.pop()\n",
    "\n",
    "        return len(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        num = 0\n",
    "        for i in range(len(logs)):\n",
    "            if logs[i]!=\"./\" and logs[i]!=\"../\":\n",
    "                num = num+1\n",
    "            elif logs[i]==\"../\":\n",
    "                if num==0:\n",
    "                    continue\n",
    "                num = num-1\n",
    "        \n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for x in logs:\n",
    "            if x == \"../\":\n",
    "                ans = max(0, ans-1)\n",
    "            elif x != \"./\":\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        cur = 0\n",
    "        for log in logs:\n",
    "            match log:\n",
    "                case \"../\":\n",
    "                    cur = max(cur-1,0)\n",
    "                case \"./\":\n",
    "                    pass\n",
    "                case _:\n",
    "                    cur+=1\n",
    "        return cur\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        s = []\n",
    "        for log in logs:\n",
    "            if log == '../':\n",
    "                if s:\n",
    "                    s.pop(-1)\n",
    "            elif log == './':\n",
    "                pass\n",
    "            else:\n",
    "                s.append(log)\n",
    "        return len(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        step = 0\n",
    "        for i in logs:\n",
    "            if i == \"../\":\n",
    "                if step != 0:\n",
    "                    step -= 1\n",
    "            elif i == \"./\":\n",
    "                pass\n",
    "            else:\n",
    "                step += 1\n",
    "        return step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        back = '../'\n",
    "        stay = './'\n",
    "        for log in logs:\n",
    "            if log == back:\n",
    "                if ans:\n",
    "                    ans -= 1\n",
    "                continue\n",
    "            if not log == stay:\n",
    "                ans += 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        return reduce(lambda x, y: max(x+{\"./\": 0, \"../\": -1}[y] if y in (\"./\", \"../\") else x+1, 0), logs, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        count = 0\n",
    "        for i in logs:\n",
    "            if i == '../' :count= max(count-1,0)\n",
    "            elif i !='./' and i != '../':count+=1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "\n",
    "        depth = 0\n",
    "\n",
    "\n",
    "        for log in logs:\n",
    "\n",
    "            if log == \"./\":\n",
    "                continue\n",
    "            elif log == \"../\":\n",
    "                if depth >= 1:\n",
    "                    depth -= 1\n",
    "                else:\n",
    "                    continue\n",
    "            else:\n",
    "                depth += 1\n",
    "\n",
    "        return depth if depth >= 0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        step = 0\n",
    "        for i in logs:\n",
    "            if i == './' or (i =='../' and step == 0):\n",
    "                pass\n",
    "            elif i == '../' :\n",
    "                step -= 1\n",
    "            else:\n",
    "                step += 1\n",
    "        return step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        stack = []\n",
    "        for x in logs:\n",
    "            if stack and x == \"../\":\n",
    "                stack.pop()\n",
    "            elif x != \"./\" and x != \"../\":\n",
    "                stack.append(x)\n",
    "        return len(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        depth = 0\n",
    "        for log in logs:\n",
    "            if log == \"../\":\n",
    "                depth -= 1\n",
    "                depth = max(0, depth)\n",
    "            elif log == \"./\":\n",
    "                pass\n",
    "            else:\n",
    "                depth += 1\n",
    "        return abs(depth)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        res = 0\n",
    "        for i in logs:\n",
    "            if i=='../':\n",
    "                if res:\n",
    "                    res -= 1 \n",
    "            elif i=='./':\n",
    "                continue\n",
    "            else:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        depth = 0\n",
    "        for log in logs:\n",
    "            if log != './' and log != '../':\n",
    "                depth += 1\n",
    "            elif log == '../' and depth != 0:\n",
    "                depth -= 1\n",
    "        return depth\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def minOperations(self, logs: List[str]) -> int:\r\n",
    "        cnt = 0\r\n",
    "        for log in logs:\r\n",
    "            if log == '../' and cnt > 0:\r\n",
    "                cnt -= 1\r\n",
    "            elif log == './' or log == '../' and cnt == 0:\r\n",
    "                continue\r\n",
    "            else:\r\n",
    "                cnt += 1\r\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        s = []\n",
    "        for log in logs:\n",
    "            if log == '../':\n",
    "                if s:\n",
    "                    s.pop(-1)\n",
    "            elif log == './':\n",
    "                pass\n",
    "            else:\n",
    "                s.append(log)\n",
    "        return len(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        stack = []\n",
    "        for log in logs:\n",
    "            if log == \"../\":\n",
    "                if stack:\n",
    "                    stack.pop()\n",
    "            elif log == \"./\":\n",
    "                continue\n",
    "            # elif log in stack:\n",
    "            #     idx = stack.index(log)\n",
    "            #     stack = stack[:idx+1]\n",
    "            else:\n",
    "                stack.append(log)\n",
    "        return len(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        step = 0\n",
    "\n",
    "        for i in logs:\n",
    "\n",
    "            if i.startswith(\"../\"):\n",
    "                step = step - 1\n",
    "            elif i.startswith(\"./\"):\n",
    "                continue\n",
    "            else:\n",
    "                step = step + 1\n",
    "\n",
    "            if step < 0:\n",
    "                step = 0\n",
    "\n",
    "        return step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        steps = 0\n",
    "        for i in range(len(logs)):\n",
    "            filename = logs[i].split('/')\n",
    "            filename = filename[0]\n",
    "            if filename==\"..\" and steps==0:continue\n",
    "            elif filename==\"..\" and steps!=0:steps -= 1\n",
    "            elif filename==\".\":continue\n",
    "            else:steps += 1\n",
    "        return steps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for log in logs:\n",
    "            if log==\"../\":\n",
    "                ans-=1\n",
    "            elif log==\"./\":\n",
    "                ans+=0\n",
    "            else:\n",
    "                ans+=1\n",
    "            #如果 ans 是-的就置为 0。不然会把+1 的抵消 \n",
    "            if ans>0:\n",
    "                ans=ans\n",
    "            else:\n",
    "                ans=0\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        k=0\n",
    "        for i in logs:\n",
    "            if i ==\"../\":\n",
    "                if k>0:\n",
    "                    k-=1\n",
    "            elif i==\"./\":\n",
    "                continue\n",
    "            else:\n",
    "                k=k+1\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        count = 0\n",
    "        for i in logs:\n",
    "            if i == '../' :count= max(count-1,0)\n",
    "            elif i !='./' and i != '../':count+=1\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        res = 0\n",
    "        for li in logs:\n",
    "            if li == \"./\":\n",
    "                continue\n",
    "            elif li == \"../\":\n",
    "                if res > 0:\n",
    "                    res -= 1\n",
    "            else:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for log in logs:\n",
    "            if log==\"../\":\n",
    "                ans-=1\n",
    "            elif log==\"./\":\n",
    "                ans+=0\n",
    "            else:\n",
    "                ans+=1\n",
    "            if ans>0:\n",
    "                ans=ans\n",
    "            else:\n",
    "                ans=0\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for x in logs:\n",
    "            if x == \"../\":\n",
    "                if ans:\n",
    "                    ans -= 1\n",
    "            elif x == \"./\":\n",
    "                continue\n",
    "            else:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        res = 0\n",
    "        for i in logs:\n",
    "            if i == './':\n",
    "                continue\n",
    "            elif i == '../':\n",
    "                res = max(0,res -1 )\n",
    "            else:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for log in logs:\n",
    "            if \"../\" == log:\n",
    "                ans -= 1\n",
    "            elif \"./\" == log:\n",
    "                continue\n",
    "            else:\n",
    "                ans += 1\n",
    "            if ans<0:\n",
    "                ans = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        loc = 0\n",
    "        for step in logs:\n",
    "            if step == '../':  loc = loc-1 if loc>0 else 0\n",
    "            elif step =='./': pass\n",
    "            else: loc += 1\n",
    "        return loc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        res=0\n",
    "        n=len(logs)\n",
    "        for i in range(n):\n",
    "            if logs[i]!='./' and logs[i]!='../':\n",
    "                res+=1\n",
    "            elif logs[i]=='../':\n",
    "                res=max(0,res-1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        stack = []\n",
    "        for i in logs:\n",
    "            if i == '../' and stack:\n",
    "                stack.pop()\n",
    "            elif i == './' or i == '../':\n",
    "                continue\n",
    "            else:\n",
    "                stack.append(i)\n",
    "        return len(stack)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        res =0\n",
    "        for v in logs:\n",
    "            if v not in ['./','../']: res+=1\n",
    "            if v =='../' and res>0 :res-=1\n",
    "        if res>=0: return res\n",
    "        else: return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        n=len(logs)\n",
    "        stack=[]\n",
    "        for i in range(n):\n",
    "            if logs[i]==\"./\":\n",
    "                continue\n",
    "            if logs[i]==\"../\" and len(stack)>0:\n",
    "                stack.pop()\n",
    "                continue\n",
    "            if logs[i]==\"../\" and len(stack)==0:\n",
    "                continue\n",
    "            else:\n",
    "                stack.append(logs[i])\n",
    "        return len(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        count = 0\n",
    "        for i in logs:\n",
    "            if i == '../' and count!=0:count-=1\n",
    "            elif i !='./' and i != '../':count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for l in logs:\n",
    "            if l == '../':\n",
    "                ans = max(0, ans-1)\n",
    "            elif l != './':\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for i in logs:\n",
    "            if \"..\" in i:\n",
    "                if ans == 0:\n",
    "                    pass\n",
    "                else:\n",
    "                    ans -= 1\n",
    "            elif \".\" in i:\n",
    "                pass\n",
    "            else:\n",
    "                ans += 1\n",
    "        return ans if ans>=0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        depth = 0\n",
    "        for log in logs:\n",
    "            if log == \"../\":\n",
    "                if depth > 0:\n",
    "                    depth -= 1\n",
    "            elif log != \"./\":\n",
    "                depth += 1\n",
    "        return depth\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        stack = []\n",
    "        for x in logs:\n",
    "            if x == '../' and stack:\n",
    "                stack.pop()\n",
    "            elif x == '../' and stack == [] or x == './':\n",
    "                pass\n",
    "            else:\n",
    "                stack.append(x)\n",
    "            #print(x)\n",
    "            #print(stack)\n",
    "        return len(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        step=0\n",
    "        for i in logs:\n",
    "            if \"../\" == i:\n",
    "                step -= 1 if step > 0 else 0\n",
    "            elif \"./\" == i:\n",
    "                continue\n",
    "            else:\n",
    "                step += 1\n",
    "        return step if step >=0 else 0\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minOperations(self, logs: List[str]) -> int:\n",
    "        mo = 0\n",
    "        for i in range(len(logs)):\n",
    "            if logs[i] == \"../\":\n",
    "                if mo != 0 :\n",
    "                    mo -= 1\n",
    "            elif logs[i] == \"./\":\n",
    "                mo = mo\n",
    "            else:\n",
    "                mo += 1\n",
    "        return mo"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
